<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */


// Call LeaveTypeTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "LeaveTypeTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once "testConf.php";

$_SESSION['WPATH'] = WPATH;

require_once "LeaveType.php";
require_once ROOT_PATH."/lib/confs/Conf.php";
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';

/**
 * Test class for LeaveType.
 * Generated by PHPUnit_Util_Skeleton on 2006-10-18 at 10:36:24.
 */
class LeaveTypeTest extends PHPUnit_Framework_TestCase {
    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */

    public $classLeaveType = null;
    public $connection = null;

    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("LeaveTypeTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
    	$this->classLeaveType = new LeaveType();

    	$conf = new Conf();

    	$this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);

        mysql_select_db($conf->dbname);

        mysql_query("TRUNCATE TABLE `hs_hr_leavetype`");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY011', 'Medical', 1)");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY012', 'Medicals', 1)");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY013', 'Medicalx', 1)");

        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY014', 'Casual', 1)");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY015', 'Casual', 1)");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY016', 'Casual', 1)");


        
        UniqueIDGenerator::getInstance()->initTable();
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {

    	 mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY011'", $this->connection);
    	 mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY012'", $this->connection);
    	 mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY013'", $this->connection);
    	 
         mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY014'", $this->connection);
         mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY015'", $this->connection);
         mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY016'", $this->connection);

    }

   public function testAddLeaveType() {

    	$this->classLeaveType->setLeaveTypeName("Anual");

    	$res = $this->classLeaveType->addLeaveType();
    	$id = $this->classLeaveType->getLeaveTypeId();

    	$res = $this->classLeaveType->retriveLeaveType($id);
		$this->assertNotNull($res);

        $expected = array($id, 'Anual');

        $this->assertEquals($res[0]->getLeaveTypeId(), $expected[0], "Didn't return expected result 1");
        $this->assertEquals($res[0]->getLeaveTypeName(), $expected[1], "Didn't return expected result 2");



        $this->assertEquals(count($res), 1, "Number of records found is not accurate ");

   }

   public function testRetriveLeaveType() {

        $res = $this->classLeaveType->retriveLeaveType("LTY017");

        $this->assertEquals($res, null, "Retured non exsistant record ");
   }



   public function testRetriveLeaveAccuracy() {

        $res = $this->classLeaveType->retriveLeaveType("LTY011");

        $expected[0] = array('Anual', 'LTY011');


        $this->assertNotNull($res, "No record found ");



   }


   public function testEditLeavetype () {

   		$this->classLeaveType->setLeaveTypeName("New Medicals");
   		$this->classLeaveType->setLeaveTypeId("LTY012");
   		$res = $this->classLeaveType->editLeaveType();

   		$res = $this->classLeaveType->retriveLeaveType("LTY012");

   		$expected = array('LTY012', 'New Medicals');

        $this->assertEquals($res[0]->getLeaveTypeId(), $expected[0], "Didn't return expected result 1");
        $this->assertEquals($res[0]->getLeaveTypeName(), $expected[1], "Didn't return expected result 2");


        $this->assertNotNull($res, "No record found ");

   }


   public function testDeleteLeaveType () {

   		$this ->classLeaveType->setLeaveTypeId("LTY012");

   		$res = $this->classLeaveType->deleteLeaveType("LTY012");

   		$res = $this->classLeaveType->retriveLeaveType("LTY012");


        $expected = array('LTY012', 'Medicals' , $res[0]->unAvailableStatusFlag);

        $this->assertEquals($res[0]->getLeaveTypeId(), $expected[0], "Didn't return expected result 1");
        $this->assertEquals($res[0]->getLeaveTypeName(), $expected[1], "Didn't return expected result 2");


        $this->assertNotNull($res, "No record found ");
   }

   public function testFetchLeave() {
        $res = $this->classLeaveType->fetchLeaveTypes();

        $this->assertNotNull($res, "No record found ");
    }


    public function testFetchLeaveAccuracy() {

        $res = $this->classLeaveType->fetchLeaveTypes();

        $expected[] = array("LTY014", "Casual");
        $expected[] = array("LTY015", "Casual");
        $expected[] = array("LTY016", "Casual");
        $expected[] = array("LTY011", "Medical");
        $expected[] = array("LTY012", "Medicals");
        $expected[] = array("LTY013", "Medicalx");

        for ($i=0; $i < count($res); $i++) {
        	$this->assertEquals($expected[$i][0], $res[$i]->getLeaveTypeId(), "Didn't return expected result 1");
        	$this->assertEquals($expected[$i][1], $res[$i]->getLeaveTypeName(), "Didn't return expected result 2");
        }
    }

    public function testGetLeaveTypeWithName() {

    	// Undeleted leave types
    	$typeList = $this->classLeaveType->getLeaveTypeWithName("Medical");
    	$this->assertNotNull($typeList);
		$this->assertEquals(1, count($typeList));
		$type = $typeList[0];

    	$this->assertEquals("LTY011", $type->getLeaveTypeId());
    	$this->assertEquals(1, $type->getLeaveTypeAvailable());

    	// Deleted leave types
    	$this->assertTrue(mysql_query("UPDATE `hs_hr_leavetype` SET available_flag = 0 WHERE leave_type_id='LTY011'"));

    	$typeList = $this->classLeaveType->getLeaveTypeWithName("Medical");
    	$this->assertNull($typeList);

    	$typeList = $this->classLeaveType->getLeaveTypeWithName("Medical", true);
    	$this->assertNotNull($typeList);
		$this->assertEquals(1, count($typeList));
		$type = $typeList[0];

    	$this->assertEquals("LTY011", $type->getLeaveTypeId());
    	$this->assertEquals(0, $type->getLeaveTypeAvailable());

		// Unavailable leave type name
    	$typeList = $this->classLeaveType->getLeaveTypeWithName("Annual");
    	$this->assertNull($typeList);

                //Chek for same leave type error, checks if its returning only one record
        $typeList = $this->classLeaveType->getLeaveTypeWithName("Casual");
        $this->assertEquals(1, count($typeList));

        // test if the most recent levetype is selected
        $myres=mysql_query("SELECT * FROM `hs_hr_leavetype` WHERE `leave_type_name` = 'Casual' ORDER BY `leave_type_id` DESC LIMIT 0,1", $this->connection);
        $myarr=mysql_fetch_array($myres);
        $this->assertEquals('LTY016',$myarr[0]);

    }

}

// Call LeaveTypeTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "LeaveTypeTest::main") {
    LeaveTypeTest::main();
}
?>
